#!/bin/bash
###SHELLPACK preamble wrf 3.8.1
WEB_LOCATION="https://www2.mmm.ucar.edu/wrf/src"
MIRROR_LOCATION="$WEBROOT/wrf"

###SHELLPACK parseargBegin
###SHELLPACK parseargEnd

MPI_SWITCH="--disable-mpi"
if [ "$WRF_OPENMPI_VERSION" != "" ]; then
	###SHELLPACK mpi_setup_deps WRF_OPENMPI_VERSION
	###SHELLPACK mpi_setup_env WRF_OPENMPI_VERSION WRF_MPI_PATH WRF_MPI_LIBPATH
	MPI_SWITCH="--enable-mpi"
fi

install-depends glibc-devel
if [ ! -e /usr/include/rpc/types.h ]; then
	install-depends libtirpc-devel
	USE_TIRPC=yes
fi

###SHELLPACK sources_fetch WRFV${VERSION}.TAR.gz wrf-${VERSION}-installed
###SHELLPACK build_start wrf-${VERSION}-installed

# Fix known build issue
file_fetch https://www2.mmm.ucar.edu/wrf/src/fix/module_cu_g3_random_seed_fix.F.gz	\
	$MIRROR_LOCATION/module_cu_g3_random_seed_fix.F.gz				\
	module_cu_g3_random_seed_fix.F.gz
tar -xf module_cu_g3_random_seed_fix.F.gz || die "Failed to unpack module_cu_g3_random_seed_fix.F.gz"
mv module_cu_g3.F phys/

# Environment setup
export NETCDF="$SHELLPACK_SOURCES/wrfdeps-installed"
export HDF5="$SHELLPACK_SOURCES/wrfdeps-installed"

# Configure parameters
# aarch64: 30. (serial)  31. (smpar)  32. (dmpar)  33. (dm+sm)   GNU (gfortran/gcc)
# x86_64:  32. (serial)  33. (smpar)  34. (dmpar)  35. (dm+sm)   GNU (gfortran/gcc)

MPISUPPORT=no
OFFSET=0
case $WRF_PAR in
smpar)
	OFFSET=1
	;;
dmpar)
	MPISUPPORT=yes
	OFFSET=2
	;;
dm+sm)
	MPISUPPORT=yes
	OFFSET=3
	;;
esac

CHOICE=99
case `uname -m` in
x86_64)
	CHOICE=$((32+$OFFSET))
	;;
aarch64)
	grep -q "Linux x86_64 aarch64" arch/configure_new.defaults
	if [ $? -ne 0 ]; then
		sed -i -e 's/Linux x86_64/Linux x86_64 aarch64/' arch/configure_new.defaults
	fi
	CHOICE=$((30+$OFFSET))
	;;
*)
	die "Unrecognised arch"
	;;
esac

# An interactive configure script
echo WRF configure choice: $CHOICE
EXPECT_SCRIPT=`mktemp`
cat > $EXPECT_SCRIPT << EOC
spawn ./configure
expect "Enter selection" { exp_send "$CHOICE\\r" }
expect "Compile for nesting" { exp_send "0\\r" }
expect {
	eof			{ exit 0 }
}
EOC
expect -f $EXPECT_SCRIPT
rm $EXPECT_SCRIPT
if [ ! -e configure.wrf ]; then
	die "configure.wrf does not exist after configure step"
fi
cp configure.wrf configure.wrf.orig

build_callback

if [ "$MPISUPPORT" = "yes" ]; then
	# Configure for openmpi2 and later
	sed -i -e "s/^DM_FC .*/& -DMPI2_SUPPORT/" configure.wrf
	sed -i -e "s/^DM_CC .*/& -DMPI2_SUPPORT/" configure.wrf
fi

# Additional configuration
LDFLAGS_EXTRA+=" -L${WRF_MPI_LIBPATH} -L${NETCDF}/$MMTESTS_LIBDIR -Wl,-rpath=${WRF_MPI_LIBPATH} -Wl,-rpath=${NETCDF}/$MMTESTS_LIBDIR -lnetcdf -lnetcdff -lhdf5_hl -lhdf5 -lz"
export LD_LIBRARY_PATH=${NETCDF}/$MMTESTS_LIBDIR:${HDF}/$MMTESTS_LIBDIR:$LD_LIBRARY_PATH
LDFLAGS_EXTRA_ESCAPED=`echo "$LDFLAGS_EXTRA" | sed -e 's/\//\\\\\//g'`
sed -i -e "s/^LDFLAGS_LOCAL.*/LDFLAGS_LOCAL   =       $LDFLAGS_EXTRA_ESCAPED/" configure.wrf
sed -i -e "s/\/lib\/cpp/cpp/" configure.wrf

if [ "$USE_TIRPC" = "yes" ]; then
	sed -i -e "s/^CFLAGS_LOCAL.*/& -I\/usr\/include\/tirpc/" configure.wrf
	sed -i -e "s/^LDFLAGS_LOCAL.*/& -ltirpc/" configure.wrf
fi

./compile wrf || die "Failed to build wrf"
./compile em_real || die "Failed to build em_real"

echo wrf installed successfully
exit $SHELLPACK_SUCCESS
